//
//  AppDelegate.swift
//  JCTest3
//
//  Created by Chen on 09/10/2022.
//

import Cocoa

@main
class AppDelegate: NSObject, NSApplicationDelegate {

    var window: NSWindow?
    var mainWC: NSWindowController?

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        
        print("===========applicationDidFinishLaunching==========")
        self.setupCustomWindow()
        
        
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }


}

extension AppDelegate {
    
    // 设置自定义窗口
    func setupCustomWindow() {
        
        /*
        // 关闭默认的窗口，使用自己自定义的窗口
        NSApplication.shared.windows.last?.close()
        let vc = ViewController()
        // 默认打开时窗口的大小
//        let windowDSize = NSRect(x: 100, y: 100, width: 1000, height: 800)
        // 屏幕2/3大小
        let screenFrame = NSScreen.main?.frame ?? NSRect.zero
//        let zoomSize = NSRect(x: 0.66 * screenFrame.minX, y: 0.66 * screenFrame.minY, width: 0.66 * screenFrame.width, height: 0.66 * screenFrame.height)
        let zoomSize1 = NSRect(x: 0, y: 0, width: 0.66 * screenFrame.width, height: 0.66 * screenFrame.height)
        
        let view = NSView(frame: zoomSize1)
//        let view = NSView(frame: screenFrame)
//         设置view的颜色
        view.setupViewBackgroundColor(color: NSColor(red: 0.9, green: 1.0, blue: 0.9, alpha: 1.0))
        vc.view = view
        
        // 测试加一个子视图
//        let newView = NSView(frame: NSRect(x: 100, y: 100, width: 100, height: 40))
//        newView.setupViewBackgroundColor(color: NSColor(red: 1.0, green: 0.6, blue: 0.6, alpha: 1.0))
//        view.addSubview(newView)

        let style: NSWindow.StyleMask = [.titled, .miniaturizable, .resizable, .closable]
        let backingStoreType: NSWindow.BackingStoreType = .retained
        self.window = NSWindow.init(contentRect: screenFrame, styleMask: style, backing: backingStoreType, defer: true)
//        self.window = NSWindow.init(contentRect: NSScreen.main?.frame ?? NSRect.zero, styleMask: style, backing: backingStoreType, defer: true)
        self.window?.orderFront(nil)
//        self.window?.center()
//        self.window?.contentViewController = vc
        
        print("================call==============")
//        self.window?.makeKeyAndOrderFront(nil)
        
        // 窗口的标题
        self.window?.title = "JCtest"
    */
        // 关闭默认的窗口
        NSApplication.shared.windows.last?.close()
        let screenFrame = NSScreen.main?.frame ?? NSRect.zero
        print("width: \(screenFrame.width)========height: \(screenFrame.height)")
        let adjustSize = NSRect(x: 0, y: 0, width: 0.66 * screenFrame.width, height: 0.66 * screenFrame.height)
        let style: NSWindow.StyleMask = [.titled, .miniaturizable, .resizable, .closable]
        let backingStoreType: NSWindow.BackingStoreType = .buffered
        let myWindow = NSWindow.init(contentRect: adjustSize, styleMask: style, backing: backingStoreType, defer: true)
        
        myWindow.center()
        myWindow.makeKeyAndOrderFront(nil)
        myWindow.title = "myMacOS app v1.0.0"
        myWindow.minSize = NSSize(width: 300, height: 240)
        myWindow.maxSize = NSSize(width: 1440, height: 900)
        // 自定义一个main controller
        let mainVc = JCMainViewController()
//        let mainVc = JCNSButtonRelatedController() // 测试
//        let mainVc = JCSplitViewTestXibController(nibName: "JCSplitViewTestXibController", bundle: Bundle.main)
        myWindow.contentViewController = mainVc
//        myWindow.showsResizeIndicator = true
//        myWindow.styleMask = .miniaturizable

        self.mainWC = NSWindowController(window: myWindow)
        self.mainWC?.showWindow(nil)
    }
    
}
